﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using DataAccess;
using System.Data.SqlClient;
namespace Business
{
    public class BusinessTypeBusiness
    {


        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static BusinessTypeBusiness Instance { get; private set; }
        /// <summary>
        /// Initializes the <see cref="BusinessTypeBusiness" /> class.
        /// </summary>
        static BusinessTypeBusiness() { Instance = new BusinessTypeBusiness(); }

        /// <summary>
        /// Searches the business.
        /// </summary>
        /// <param name="businessName">Name of the business.</param>
        /// <param name="sicCode">The sic code.</param>
        /// <returns></returns>
        public Collection<Entities.Business> SearchBusiness(string businessName, string sicCode, out string message)
        {
            if (businessName == null || sicCode == null)
            {
                message = "Input not null";
                return null;
            }
            try
            {
                message = BusinessDataAccess.Instance.ListBusiness(businessName, sicCode).Count.ToString();
                return BusinessDataAccess.Instance.ListBusiness(businessName, sicCode);
            }
            catch (SqlException exception)
            {
                message = exception.Message;
                return null;
            }

        }

        public Entities.Business GetBusiness(string businessId)
        {
            if(!string.IsNullOrEmpty(businessId) )
            {
                try
                {
                    return BusinessDataAccess.Instance.GetBusiness(businessId);
                }
                catch (Exception)
                {

                    return null;
                }
            }
            return null;

        }
        /// <summary>
        /// Pagings the business.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="lstBusiness">The LST business.</param>
        /// <returns></returns>
        public Collection<Entities.Business> PagingBusiness(int pageNumber, int pageSize, Collection<Entities.Business> lstBusiness)
        {

            if (lstBusiness != null)
            {
                if (pageSize > 0)
                {
                    var totalpage = lstBusiness.Count % pageSize == 0 ? lstBusiness.Count / pageSize : lstBusiness.Count / pageSize + 1;
                    if (pageNumber <= totalpage && pageNumber > 0)

                        return new Collection<Entities.Business>(lstBusiness.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList());

                }
                return null;
            }
            return null;
        }
    }
}
